import os
import sys

from PyQt6.QtCore import QThread, QObject, pyqtSignal, QStringListModel
from PyQt6.QtWidgets import QApplication, QMainWindow, QLineEdit, QListView, QPushButton, QWidget, QVBoxLayout
from PyQt6.uic import loadUi

import main_model
from global_config import workflows_dir, root
from ui.Create_page import Create_page


class UvicornThread(QObject):
    finished = pyqtSignal(int)

    def run(self):
        # server.run("0.0.0.0", 8063)
        # # 使用subprocess.run来启动uvicorn服务
        # process = subprocess.run(
        #     ['uvicorn', api_app, '--host', host, '--port', port], capture_output=True, text=True
        # )
        # 当uvicorn服务结束时，发出finished信号
        self.finished.emit("exit")


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.createpage: Create_page = None
        self.content_view: QVBoxLayout = None
        self.tab_view: QVBoxLayout = None
        self.home_tab: QPushButton = None
        self.create_tab: QPushButton = None
        self.pre_tab: QPushButton = None
        self.setting_tab: QPushButton = None
        self.pushButton = None
        self.centralwidget: QWidget = None
        self.draft_btn: QPushButton = None
        self.draft_edit: QLineEdit = None
        self.listView: QListView = None
        self.project_edit: QLineEdit = None
        self.initUI()
        self.start_service()

    def initUI(self):
        self.createpage = None
        self.main_page_ui = loadUi('./source/main_page.ui', self)

        self.home_tab.clicked.connect(
            lambda: self.check_tab_view(0)
        )
        self.create_tab.clicked.connect(
            lambda: self.check_tab_view(1)
        )
        self.pre_tab.clicked.connect(
            lambda: self.check_tab_view(2)
        )
        self.setting_tab.clicked.connect(
            lambda: self.check_tab_view(3)
        )

        self.create_tab.click()

    def start_service(self):
        # 创建一个线程来运行uvicorn服务
        self.thread = QThread(self)
        self.uvicorn_thread = UvicornThread()
        self.uvicorn_thread.moveToThread(self.thread)

        # 连接信号和槽
        self.thread.started.connect(self.uvicorn_thread.run)
        self.uvicorn_thread.finished.connect(self.on_finished)
        self.thread.start()

    def on_finished(self, returncode):
        # 服务结束时的回调
        print(f'Uvicorn server finished with return code: {returncode}')

    # 切换标签
    def check_tab_view(self, index):
        self.home_tab.setChecked(index == 0)
        self.create_tab.setChecked(index == 1)
        self.pre_tab.setChecked(index == 2)
        self.setting_tab.setChecked(index == 3)

        def clearview():
            if self.createpage is not None:
                self.content_view.removeWidget(self.createpage)
                self.createpage.deleteLater()
                self.createpage = None

        clearview()
        if index == 1:
            self.createpage = Create_page(self)
            self.content_view.addWidget(self.createpage)
        elif index == 2:
            pass
        elif index == 3:
            pass
        else:
            pass


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec())
